﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using DBUtility;
using System.Collections.Generic;

/// <summary>
/// DB 的摘要说明

/// </summary>
public class DB
{
    public static String code = ConfigurationManager.AppSettings["CODE"];
    public static string ERPConnectionString = ConfigurationManager.ConnectionStrings["ERPConnectionString"].ConnectionString;
    public static string WEBConnectionString = ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString;
    public DB()
    {

    }
    #region 验证
    public static bool CheckCode(string p)
    {
        return p.Trim() == code.Trim() ? true : false;
    }
    #endregion

    #region 会员注册
    #region 写入ERP
    public static string InsertVIP(string sddm, string gkdm, string gkmc, string gksex, string pwd, string emial, string address, string postCode, string mobile, string phone, string ReCommendCustomer, string Nike, string province, string city, string area)
    {
        string result = gkdm;
        SqlParameter[] p =
            {
                new SqlParameter("@KHDM", SqlDbType.VarChar,20),
                new SqlParameter("@GKDM", SqlDbType.VarChar,20),
                new SqlParameter("@GKMC", SqlDbType.VarChar,50),
                new SqlParameter("@GKSEX", SqlDbType.VarChar,20),
                new SqlParameter("@PWD", SqlDbType.VarChar,100),
                new SqlParameter("@Email", SqlDbType.VarChar,50),
                new SqlParameter("@address", SqlDbType.VarChar,20),
                new SqlParameter("@postCode", SqlDbType.VarChar,20),
                new SqlParameter("@mobile", SqlDbType.VarChar,20),
                new SqlParameter("@phone", SqlDbType.VarChar,20),
                new SqlParameter("@Nike", SqlDbType.VarChar,50)
            };
        p[0].Value = sddm;
        p[1].Value = gkdm;
        p[2].Value = gkmc;
        p[3].Value = gksex;
        p[4].Value = pwd;
        p[5].Value = emial;
        p[6].Value = address;
        p[7].Value = postCode;
        p[8].Value = mobile;
        p[9].Value = phone;
        p[10].Value = Nike;
        DataSet ds = SqlHelper.RunProcedure(ERPConnectionString, "WEB_InsertVIP", p, "ds");
        
        
        
        if (ds.Tables.Count > 1)
        {
            V_CUSTOMER OP_Customer = new V_CUSTOMER();
            List<Model.V_CUSTOMER> customer = OP_Customer.GetList(ds.Tables[0]) as List<Model.V_CUSTOMER>;
            foreach (Model.V_CUSTOMER cs in customer)
            {
                OP_Customer.Add(cs);
            }

            BLL.V_CUSTOMER bllcustomer = new BLL.V_CUSTOMER(); //扩展表插入信息

            bllcustomer.V_CUSTOMER_AUTO_Reg(gkdm,province,city,area,address); 

            V_VIPSET OP_VipSet = new V_VIPSET();
            List<Model.V_VIPSET> Vipsets = OP_VipSet.GetList(ds.Tables[1]) as List<Model.V_VIPSET>;
            foreach (Model.V_VIPSET vs in Vipsets)
            {                
                OP_VipSet.Add(vs);
                result = result + "|" + vs.ZK + "|" + vs.ZK2 + "|" + vs.ZK_LIMIT + "|";
            }

            Model.SYSPARAM sysparammodel = new BLL.SYSPARAM().GetModel("007"); //取注册送积分,注册推荐人送积分开关
            if (sysparammodel != null)
            {
                List<string> list = new List<string>(); //积分流水账Sql
                List<string> list1 = new List<string>();//更新会员积分Sql
                Model.IntergalTypeSet intergalTypeSetMode = BLL.IntergalTypeSet.GetSysInterTypeSet();  //取基础设置
                if (intergalTypeSetMode != null)
                {
                    
                        BLL.InergalManage inergalbll = new BLL.InergalManage();
                        DateTime time = DateTime.Now;
                        if (sysparammodel.SP_PARAM1 == 1)  //注册送积分
                        {
                            List<string> temp = inergalbll.RegInergalShare(gkdm, intergalTypeSetMode.IntergalSetExtendAttribute5, gkdm, BLL.InergalManage.InergalTypeEnum.注册积分, time.ToString(), sysparammodel, intergalTypeSetMode);
                            if (temp != null)
                            {
                                foreach (string tt in temp)
                                {
                                    list.Add(tt);
                                }
                                list1.Add("update V_Vipset set DQJF=DQJF+" + intergalTypeSetMode.IntergalSetExtendAttribute5 + " where GKDM='" + gkdm + "'");
                            }
                        }
                        if (sysparammodel.SP_PARAM14 == "1")  //启用会员类型
                        {
                            list.Add("Update V_Customer_Auto set BYZD9='" + BLL.Common.GetCustomerTypeCode() + "' where VC_CODE='" + gkdm + "'");
                        }
                        if (sysparammodel.SP_PARAM3 == 1)  //推荐机制开启
                        {
                            if (ReCommendCustomer.Length > 0)  //判断推荐人是否为空
                            {
                                List<string> temp = inergalbll.RegReCommendInergalShare(ReCommendCustomer, decimal.Parse(intergalTypeSetMode.IntergalSetExtendAttribute2), gkdm, BLL.InergalManage.InergalTypeEnum.推荐积分, time.ToString(), sysparammodel, intergalTypeSetMode);                                
                                if (temp != null)
                                {
                                    foreach (string tt in temp)
                                    {
                                        list.Add(tt);
                                    }
                                    if (sysparammodel.SP_PARAM6 == 1)
                                    {
                                        list1.Add("update V_Vipset set DQJF=DQJF+" + intergalTypeSetMode.IntergalSetExtendAttribute2 + " where GKDM='" + ReCommendCustomer + "'");
                                    }
                                }

                            }
                        }
                        InsertIntergal(list); //写入流水账
                        UpdateCustomerDqjf(list1);  //更新会员积分
                    
                }
            }
        }
        else
        {
            result = ds.Tables[0].Rows[0][0].ToString();
        }
        return result;
    }    
    #endregion
    #endregion

    #region 更新会员信息
    public static string Customer_Update(Model.V_CUSTOMER customer, Model.V_CUSTOMER_AUTO vc_auto)
    {
        string aa = VC_Customer_ERP_Update(customer);
        string bb = VC_Customer_WEB_Update(customer);
        string cc=VC_Auto_Update(vc_auto);
        return aa + bb + cc;
    }
    #region 更新erp_Customer表

    /// <summary>
        /// 更新一条数据

        /// </summary>
    private static string VC_Customer_ERP_Update(Model.V_CUSTOMER model)
    {
        int rowsAffected1;        
        
            SqlParameter[] parameters = {
                    new SqlParameter("@DM", SqlDbType.VarChar,20),
                    new SqlParameter("@GKMC", SqlDbType.VarChar,50),
                    new SqlParameter("@SEX", SqlDbType.VarChar,2),
                    new SqlParameter("@SR", SqlDbType.DateTime),
                    new SqlParameter("@BM", SqlDbType.VarChar,20),
                    new SqlParameter("@KITHMC", SqlDbType.VarChar,20),
                    new SqlParameter("@KITHSEX", SqlDbType.VarChar,2),
                    new SqlParameter("@QYDM", SqlDbType.VarChar,6),
                    new SqlParameter("@QDDM", SqlDbType.VarChar,6),
                    new SqlParameter("@CKDM", SqlDbType.VarChar,6),
                    new SqlParameter("@JDRQ", SqlDbType.DateTime),
                    new SqlParameter("@XGRQ", SqlDbType.DateTime),
                    new SqlParameter("@FKBJ", SqlDbType.Char,1),
                    new SqlParameter("@TKBJ", SqlDbType.Char,1),
                    new SqlParameter("@XFJE", SqlDbType.Decimal),
                    new SqlParameter("@XFSL", SqlDbType.Int),
                    new SqlParameter("@DQJF", SqlDbType.Int),
                    new SqlParameter("@DZ", SqlDbType.VarChar,200),
                    new SqlParameter("@YB", SqlDbType.VarChar,20),
                    new SqlParameter("@SJ", SqlDbType.VarChar,20),
                    new SqlParameter("@DH", SqlDbType.VarChar,20),
                    new SqlParameter("@CZ", SqlDbType.VarChar,20),
                    new SqlParameter("@EMAIL", SqlDbType.VarChar,50),
                    new SqlParameter("@BZ", SqlDbType.VarChar,100),
                    new SqlParameter("@SZGG", SqlDbType.VarChar,6),
                    new SqlParameter("@XZGG", SqlDbType.VarChar,6),
                    new SqlParameter("@COUNTRY", SqlDbType.VarChar,6),
                    new SqlParameter("@DISTRICT", SqlDbType.VarChar,6),
                    new SqlParameter("@CITY", SqlDbType.VarChar,6),
                    new SqlParameter("@EDUCATION", SqlDbType.VarChar,6),
                    new SqlParameter("@OCCUPATION", SqlDbType.VarChar,6),
                    new SqlParameter("@MARRY", SqlDbType.VarChar,6),
                    new SqlParameter("@EARNING", SqlDbType.VarChar,6),
                    new SqlParameter("@WORTH", SqlDbType.VarChar,6),
                    new SqlParameter("@DUDS", SqlDbType.VarChar,6),
                    new SqlParameter("@KNOWS", SqlDbType.VarChar,6),
                    new SqlParameter("@FJSX1", SqlDbType.VarChar,6),
                    new SqlParameter("@FJSX2", SqlDbType.VarChar,6),
                    new SqlParameter("@FJSX3", SqlDbType.VarChar,6),
                    new SqlParameter("@FJSX4", SqlDbType.VarChar,6),
                    new SqlParameter("@FJSX5", SqlDbType.VarChar,6),
                    new SqlParameter("@FJSX6", SqlDbType.VarChar,6),
                    new SqlParameter("@BYZD1", SqlDbType.Char,1),
                    new SqlParameter("@BYZD2", SqlDbType.Char,1),
                    new SqlParameter("@BYZD3", SqlDbType.VarChar,20),
                    new SqlParameter("@BYZD4", SqlDbType.VarChar,20),
                    new SqlParameter("@BYZD5", SqlDbType.VarChar,50),
                    new SqlParameter("@BYZD6", SqlDbType.VarChar,150),
                    new SqlParameter("@BYZD7", SqlDbType.VarChar,150),
                    new SqlParameter("@BYZD8", SqlDbType.Int),
                    new SqlParameter("@BYZD9", SqlDbType.Int),
                    new SqlParameter("@BYZD10", SqlDbType.Int),
                    new SqlParameter("@BYZD11", SqlDbType.Float),
                    new SqlParameter("@BYZD12", SqlDbType.Float),
                    new SqlParameter("@BYZD13", SqlDbType.Float),
                    new SqlParameter("@BYZD14", SqlDbType.DateTime),
                    new SqlParameter("@BYZD15", SqlDbType.DateTime),                   
                    new SqlParameter("@VERSION", SqlDbType.VarChar,10)
            };
            parameters[0].Value = model.DM;

            if (model.GKMC != null)
                parameters[1].Value = model.GKMC;
            else
                parameters[1].Value = DBNull.Value;


            if (model.SEX != null)
                parameters[2].Value = model.SEX;
            else
                parameters[2].Value = DBNull.Value;


            if (model.SR != DateTime.MinValue)
                parameters[3].Value = model.SR;
            else
                parameters[3].Value = DBNull.Value;


            if (model.BM != null)
                parameters[4].Value = model.BM;
            else
                parameters[4].Value = DBNull.Value;


            if (model.KITHMC != null)
                parameters[5].Value = model.KITHMC;
            else
                parameters[5].Value = DBNull.Value;


            if (model.KITHSEX != null)
                parameters[6].Value = model.KITHSEX;
            else
                parameters[6].Value = DBNull.Value;


            if (model.QYDM != null)
                parameters[7].Value = model.QYDM;
            else
                parameters[7].Value = DBNull.Value;


            if (model.QDDM != null)
                parameters[8].Value = model.QDDM;
            else
                parameters[8].Value = DBNull.Value;


            if (model.CKDM != null)
                parameters[9].Value = model.CKDM;
            else
                parameters[9].Value = DBNull.Value;


            if (model.JDRQ != DateTime.MinValue)
                parameters[10].Value = model.JDRQ;
            else
                parameters[10].Value = DBNull.Value;


            if (model.XGRQ != DateTime.MinValue)
                parameters[11].Value = model.XGRQ;
            else
                parameters[11].Value = DBNull.Value;


            if (model.FKBJ != null)
                parameters[12].Value = model.FKBJ;
            else
                parameters[12].Value = DBNull.Value;


            if (model.TKBJ != null)
                parameters[13].Value = model.TKBJ;
            else
                parameters[13].Value = DBNull.Value;

            parameters[14].Value = model.XFJE;
            parameters[15].Value = model.XFSL;
            parameters[16].Value = model.DQJF;

            if (model.DZ != null)
                parameters[17].Value = model.DZ;
            else
                parameters[17].Value = DBNull.Value;


            if (model.YB != null)
                parameters[18].Value = model.YB;
            else
                parameters[18].Value = DBNull.Value;


            if (model.SJ != null)
                parameters[19].Value = model.SJ;
            else
                parameters[19].Value = DBNull.Value;


            if (model.DH != null)
                parameters[20].Value = model.DH;
            else
                parameters[20].Value = DBNull.Value;


            if (model.CZ != null)
                parameters[21].Value = model.CZ;
            else
                parameters[21].Value = DBNull.Value;


            if (model.EMAIL != null)
                parameters[22].Value = model.EMAIL;
            else
                parameters[22].Value = DBNull.Value;


            if (model.BZ != null)
                parameters[23].Value = model.BZ;
            else
                parameters[23].Value = DBNull.Value;


            if (model.SZGG != null)
                parameters[24].Value = model.SZGG;
            else
                parameters[24].Value = DBNull.Value;


            if (model.XZGG != null)
                parameters[25].Value = model.XZGG;
            else
                parameters[25].Value = DBNull.Value;


            if (model.COUNTRY != null)
                parameters[26].Value = model.COUNTRY;
            else
                parameters[26].Value = DBNull.Value;


            if (model.DISTRICT != null)
                parameters[27].Value = model.DISTRICT;
            else
                parameters[27].Value = DBNull.Value;


            if (model.CITY != null)
                parameters[28].Value = model.CITY;
            else
                parameters[28].Value = DBNull.Value;


            if (model.EDUCATION != null)
                parameters[29].Value = model.EDUCATION;
            else
                parameters[29].Value = DBNull.Value;


            if (model.OCCUPATION != null)
                parameters[30].Value = model.OCCUPATION;
            else
                parameters[30].Value = DBNull.Value;


            if (model.MARRY != null)
                parameters[31].Value = model.MARRY;
            else
                parameters[31].Value = DBNull.Value;


            if (model.EARNING != null)
                parameters[32].Value = model.EARNING;
            else
                parameters[32].Value = DBNull.Value;


            if (model.WORTH != null)
                parameters[33].Value = model.WORTH;
            else
                parameters[33].Value = DBNull.Value;


            if (model.DUDS != null)
                parameters[34].Value = model.DUDS;
            else
                parameters[34].Value = DBNull.Value;


            if (model.KNOWS != null)
                parameters[35].Value = model.KNOWS;
            else
                parameters[35].Value = DBNull.Value;


            if (model.FJSX1 != null)
                parameters[36].Value = model.FJSX1;
            else
                parameters[36].Value = DBNull.Value;


            if (model.FJSX2 != null)
                parameters[37].Value = model.FJSX2;
            else
                parameters[37].Value = DBNull.Value;


            if (model.FJSX3 != null)
                parameters[38].Value = model.FJSX3;
            else
                parameters[38].Value = DBNull.Value;


            if (model.FJSX4 != null)
                parameters[39].Value = model.FJSX4;
            else
                parameters[39].Value = DBNull.Value;


            if (model.FJSX5 != null)
                parameters[40].Value = model.FJSX5;
            else
                parameters[40].Value = DBNull.Value;


            if (model.FJSX6 != null)
                parameters[41].Value = model.FJSX6;
            else
                parameters[41].Value = DBNull.Value;


            if (model.BYZD1 != null)
                parameters[42].Value = model.BYZD1;
            else
                parameters[42].Value = DBNull.Value;


            if (model.BYZD2 != null)
                parameters[43].Value = model.BYZD2;
            else
                parameters[43].Value = DBNull.Value;


            if (model.BYZD3 != null)
                parameters[44].Value = model.BYZD3;
            else
                parameters[44].Value = DBNull.Value;


            if (model.BYZD4 != null)
                parameters[45].Value = model.BYZD4;
            else
                parameters[45].Value = DBNull.Value;


            if (model.BYZD5 != null)
                parameters[46].Value = model.BYZD5;
            else
                parameters[46].Value = DBNull.Value;


            if (model.BYZD6 != null)
                parameters[47].Value = model.BYZD6;
            else
                parameters[47].Value = DBNull.Value;


            if (model.BYZD7 != null)
                parameters[48].Value = model.BYZD7;
            else
                parameters[48].Value = DBNull.Value;

            parameters[49].Value = model.BYZD8;
            parameters[50].Value = model.BYZD9;
            parameters[51].Value = model.BYZD10;
            parameters[52].Value = model.BYZD11;
            parameters[53].Value = model.BYZD12;
            parameters[54].Value = model.BYZD13;

            if (model.BYZD14 != DateTime.MinValue)
                parameters[55].Value = model.BYZD14;
            else
                parameters[55].Value = DBNull.Value;


            if (model.BYZD15 != DateTime.MinValue)
                parameters[56].Value = model.BYZD15;
            else
                parameters[56].Value = DBNull.Value;

           

            if (model.VERSION != null)
                parameters[57].Value = model.VERSION;
            else
                parameters[57].Value = DBNull.Value;

        SqlHelper.RunProcedure(ERPConnectionString, "sp_V_CUSTOMER_Update", parameters, out rowsAffected1); //更新ERP        
        string result = "";
        if (rowsAffected1 > 0)
        {
            result = "1";
        }
        else
        {
            result="0";            
        }        
        return result;
    }
    #endregion

    #region 更新Web_Customer表

    private static string VC_Customer_WEB_Update(Model.V_CUSTOMER model)
    {       
        int rowsAffected2;
         SqlParameter[] parameters = {
                    new SqlParameter("@DM", SqlDbType.VarChar,20),
                    new SqlParameter("@GKMC", SqlDbType.VarChar,50),
                    new SqlParameter("@SEX", SqlDbType.VarChar,2),
                    new SqlParameter("@SR", SqlDbType.DateTime),
                    new SqlParameter("@BM", SqlDbType.VarChar,20),
                    new SqlParameter("@KITHMC", SqlDbType.VarChar,20),
                    new SqlParameter("@KITHSEX", SqlDbType.VarChar,2),
                    new SqlParameter("@QYDM", SqlDbType.VarChar,6),
                    new SqlParameter("@QDDM", SqlDbType.VarChar,6),
                    new SqlParameter("@CKDM", SqlDbType.VarChar,6),
                    new SqlParameter("@JDRQ", SqlDbType.DateTime),
                    new SqlParameter("@XGRQ", SqlDbType.DateTime),
                    new SqlParameter("@FKBJ", SqlDbType.Char,1),
                    new SqlParameter("@TKBJ", SqlDbType.Char,1),
                    new SqlParameter("@XFJE", SqlDbType.Decimal),
                    new SqlParameter("@XFSL", SqlDbType.Int),
                    new SqlParameter("@DQJF", SqlDbType.Int),
                    new SqlParameter("@DZ", SqlDbType.VarChar,200),
                    new SqlParameter("@YB", SqlDbType.VarChar,20),
                    new SqlParameter("@SJ", SqlDbType.VarChar,20),
                    new SqlParameter("@DH", SqlDbType.VarChar,20),
                    new SqlParameter("@CZ", SqlDbType.VarChar,20),
                    new SqlParameter("@EMAIL", SqlDbType.VarChar,50),
                    new SqlParameter("@BZ", SqlDbType.VarChar,100),
                    new SqlParameter("@SZGG", SqlDbType.VarChar,6),
                    new SqlParameter("@XZGG", SqlDbType.VarChar,6),
                    new SqlParameter("@COUNTRY", SqlDbType.VarChar,6),
                    new SqlParameter("@DISTRICT", SqlDbType.VarChar,6),
                    new SqlParameter("@CITY", SqlDbType.VarChar,6),
                    new SqlParameter("@EDUCATION", SqlDbType.VarChar,6),
                    new SqlParameter("@OCCUPATION", SqlDbType.VarChar,6),
                    new SqlParameter("@MARRY", SqlDbType.VarChar,6),
                    new SqlParameter("@EARNING", SqlDbType.VarChar,6),
                    new SqlParameter("@WORTH", SqlDbType.VarChar,6),
                    new SqlParameter("@DUDS", SqlDbType.VarChar,6),
                    new SqlParameter("@KNOWS", SqlDbType.VarChar,6),
                    new SqlParameter("@FJSX1", SqlDbType.VarChar,6),
                    new SqlParameter("@FJSX2", SqlDbType.VarChar,6),
                    new SqlParameter("@FJSX3", SqlDbType.VarChar,6),
                    new SqlParameter("@FJSX4", SqlDbType.VarChar,6),
                    new SqlParameter("@FJSX5", SqlDbType.VarChar,6),
                    new SqlParameter("@FJSX6", SqlDbType.VarChar,6),
                    new SqlParameter("@BYZD1", SqlDbType.Char,1),
                    new SqlParameter("@BYZD2", SqlDbType.Char,1),
                    new SqlParameter("@BYZD3", SqlDbType.VarChar,20),
                    new SqlParameter("@BYZD4", SqlDbType.VarChar,20),
                    new SqlParameter("@BYZD5", SqlDbType.VarChar,50),
                    new SqlParameter("@BYZD6", SqlDbType.VarChar,150),
                    new SqlParameter("@BYZD7", SqlDbType.VarChar,150),
                    new SqlParameter("@BYZD8", SqlDbType.Int),
                    new SqlParameter("@BYZD9", SqlDbType.Int),
                    new SqlParameter("@BYZD10", SqlDbType.Int),
                    new SqlParameter("@BYZD11", SqlDbType.Float),
                    new SqlParameter("@BYZD12", SqlDbType.Float),
                    new SqlParameter("@BYZD13", SqlDbType.Float),
                    new SqlParameter("@BYZD14", SqlDbType.DateTime),
                    new SqlParameter("@BYZD15", SqlDbType.DateTime),                   
                    new SqlParameter("@VERSION", SqlDbType.VarChar,10)
            };
            parameters[0].Value = model.DM;

            if (model.GKMC != null)
                parameters[1].Value = model.GKMC;
            else
                parameters[1].Value = DBNull.Value;


            if (model.SEX != null)
                parameters[2].Value = model.SEX;
            else
                parameters[2].Value = DBNull.Value;


            if (model.SR != DateTime.MinValue)
                parameters[3].Value = model.SR;
            else
                parameters[3].Value = DBNull.Value;


            if (model.BM != null)
                parameters[4].Value = model.BM;
            else
                parameters[4].Value = DBNull.Value;


            if (model.KITHMC != null)
                parameters[5].Value = model.KITHMC;
            else
                parameters[5].Value = DBNull.Value;


            if (model.KITHSEX != null)
                parameters[6].Value = model.KITHSEX;
            else
                parameters[6].Value = DBNull.Value;


            if (model.QYDM != null)
                parameters[7].Value = model.QYDM;
            else
                parameters[7].Value = DBNull.Value;


            if (model.QDDM != null)
                parameters[8].Value = model.QDDM;
            else
                parameters[8].Value = DBNull.Value;


            if (model.CKDM != null)
                parameters[9].Value = model.CKDM;
            else
                parameters[9].Value = DBNull.Value;


            if (model.JDRQ != DateTime.MinValue)
                parameters[10].Value = model.JDRQ;
            else
                parameters[10].Value = DBNull.Value;


            if (model.XGRQ != DateTime.MinValue)
                parameters[11].Value = model.XGRQ;
            else
                parameters[11].Value = DBNull.Value;


            if (model.FKBJ != null)
                parameters[12].Value = model.FKBJ;
            else
                parameters[12].Value = DBNull.Value;


            if (model.TKBJ != null)
                parameters[13].Value = model.TKBJ;
            else
                parameters[13].Value = DBNull.Value;

            parameters[14].Value = model.XFJE;
            parameters[15].Value = model.XFSL;
            parameters[16].Value = model.DQJF;

            if (model.DZ != null)
                parameters[17].Value = model.DZ;
            else
                parameters[17].Value = DBNull.Value;


            if (model.YB != null)
                parameters[18].Value = model.YB;
            else
                parameters[18].Value = DBNull.Value;


            if (model.SJ != null)
                parameters[19].Value = model.SJ;
            else
                parameters[19].Value = DBNull.Value;


            if (model.DH != null)
                parameters[20].Value = model.DH;
            else
                parameters[20].Value = DBNull.Value;


            if (model.CZ != null)
                parameters[21].Value = model.CZ;
            else
                parameters[21].Value = DBNull.Value;


            if (model.EMAIL != null)
                parameters[22].Value = model.EMAIL;
            else
                parameters[22].Value = DBNull.Value;


            if (model.BZ != null)
                parameters[23].Value = model.BZ;
            else
                parameters[23].Value = DBNull.Value;


            if (model.SZGG != null)
                parameters[24].Value = model.SZGG;
            else
                parameters[24].Value = DBNull.Value;


            if (model.XZGG != null)
                parameters[25].Value = model.XZGG;
            else
                parameters[25].Value = DBNull.Value;


            if (model.COUNTRY != null)
                parameters[26].Value = model.COUNTRY;
            else
                parameters[26].Value = DBNull.Value;


            if (model.DISTRICT != null)
                parameters[27].Value = model.DISTRICT;
            else
                parameters[27].Value = DBNull.Value;


            if (model.CITY != null)
                parameters[28].Value = model.CITY;
            else
                parameters[28].Value = DBNull.Value;


            if (model.EDUCATION != null)
                parameters[29].Value = model.EDUCATION;
            else
                parameters[29].Value = DBNull.Value;


            if (model.OCCUPATION != null)
                parameters[30].Value = model.OCCUPATION;
            else
                parameters[30].Value = DBNull.Value;


            if (model.MARRY != null)
                parameters[31].Value = model.MARRY;
            else
                parameters[31].Value = DBNull.Value;


            if (model.EARNING != null)
                parameters[32].Value = model.EARNING;
            else
                parameters[32].Value = DBNull.Value;


            if (model.WORTH != null)
                parameters[33].Value = model.WORTH;
            else
                parameters[33].Value = DBNull.Value;


            if (model.DUDS != null)
                parameters[34].Value = model.DUDS;
            else
                parameters[34].Value = DBNull.Value;


            if (model.KNOWS != null)
                parameters[35].Value = model.KNOWS;
            else
                parameters[35].Value = DBNull.Value;


            if (model.FJSX1 != null)
                parameters[36].Value = model.FJSX1;
            else
                parameters[36].Value = DBNull.Value;


            if (model.FJSX2 != null)
                parameters[37].Value = model.FJSX2;
            else
                parameters[37].Value = DBNull.Value;


            if (model.FJSX3 != null)
                parameters[38].Value = model.FJSX3;
            else
                parameters[38].Value = DBNull.Value;


            if (model.FJSX4 != null)
                parameters[39].Value = model.FJSX4;
            else
                parameters[39].Value = DBNull.Value;


            if (model.FJSX5 != null)
                parameters[40].Value = model.FJSX5;
            else
                parameters[40].Value = DBNull.Value;


            if (model.FJSX6 != null)
                parameters[41].Value = model.FJSX6;
            else
                parameters[41].Value = DBNull.Value;


            if (model.BYZD1 != null)
                parameters[42].Value = model.BYZD1;
            else
                parameters[42].Value = DBNull.Value;


            if (model.BYZD2 != null)
                parameters[43].Value = model.BYZD2;
            else
                parameters[43].Value = DBNull.Value;


            if (model.BYZD3 != null)
                parameters[44].Value = model.BYZD3;
            else
                parameters[44].Value = DBNull.Value;


            if (model.BYZD4 != null)
                parameters[45].Value = model.BYZD4;
            else
                parameters[45].Value = DBNull.Value;


            if (model.BYZD5 != null)
                parameters[46].Value = model.BYZD5;
            else
                parameters[46].Value = DBNull.Value;


            if (model.BYZD6 != null)
                parameters[47].Value = model.BYZD6;
            else
                parameters[47].Value = DBNull.Value;


            if (model.BYZD7 != null)
                parameters[48].Value = model.BYZD7;
            else
                parameters[48].Value = DBNull.Value;

            parameters[49].Value = model.BYZD8;
            parameters[50].Value = model.BYZD9;
            parameters[51].Value = model.BYZD10;
            parameters[52].Value = model.BYZD11;
            parameters[53].Value = model.BYZD12;
            parameters[54].Value = model.BYZD13;

            if (model.BYZD14 != DateTime.MinValue)
                parameters[55].Value = model.BYZD14;
            else
                parameters[55].Value = DBNull.Value;


            if (model.BYZD15 != DateTime.MinValue)
                parameters[56].Value = model.BYZD15;
            else
                parameters[56].Value = DBNull.Value;

           

            if (model.VERSION != null)
                parameters[57].Value = model.VERSION;
            else
                parameters[57].Value = DBNull.Value;    
        SqlHelper.RunProcedure(WEBConnectionString, "sp_V_CUSTOMER_Update", parameters, out rowsAffected2); //更新WEB
        string result = "";       
        if (rowsAffected2 > 0)
        {
            result += "1";
        }
        else
        {
            result += "0";
        }
        return result;
    }
    #endregion

    #region 更新Web_v_Custmoer_Auto表

    private static string VC_Auto_Update(Model.V_CUSTOMER_AUTO model)
    {
        int rowsAffected;
        SqlParameter[] parameters = {
                    new SqlParameter("@VC_CODE", SqlDbType.VarChar,50),
                    new SqlParameter("@VC_LASTLOGINTIME", SqlDbType.DateTime),
                    new SqlParameter("@VC_PWDQUESTION", SqlDbType.VarChar,200),
                    new SqlParameter("@VC_PWDANSWER", SqlDbType.VarChar,200),
                    new SqlParameter("@VC_PROVINCE", SqlDbType.VarChar,6),
                    new SqlParameter("@VC_CITY", SqlDbType.VarChar,6),
                    new SqlParameter("@VC_AREA", SqlDbType.VarChar,6),
                    new SqlParameter("@VC_ADDRESS", SqlDbType.VarChar,200),
                    new SqlParameter("@BYZD1", SqlDbType.Int),
                    new SqlParameter("@BYZD2", SqlDbType.Int),
                    new SqlParameter("@BYZD3", SqlDbType.Int),
                    new SqlParameter("@BYZD4", SqlDbType.Int),
                    new SqlParameter("@BYZD5", SqlDbType.Float),
                    new SqlParameter("@BYZD6", SqlDbType.Float),
                    new SqlParameter("@BYZD7", SqlDbType.Float),
                    new SqlParameter("@BYZD8", SqlDbType.Float),
                    new SqlParameter("@BYZD9", SqlDbType.VarChar,1000),
                    new SqlParameter("@BYZD10", SqlDbType.VarChar,1000),
                    new SqlParameter("@BYZD11", SqlDbType.VarChar,50),
                    new SqlParameter("@BYZD12", SqlDbType.VarChar,50),

                    new SqlParameter("@BYZD13", SqlDbType.DateTime),
                    new SqlParameter("@BYZD14", SqlDbType.DateTime),
                    new SqlParameter("@CustomerLevel", SqlDbType.VarChar,200),
                    new SqlParameter("@CreatDateTime", SqlDbType.DateTime),
                    new SqlParameter("@VC_Note",SqlDbType.VarChar,1000),
                      new SqlParameter("@BYZD15", SqlDbType.VarChar,50),
                    new SqlParameter("@BYZD16", SqlDbType.VarChar,50),
                    new SqlParameter("@BYZD17", SqlDbType.VarChar,50),
                    new SqlParameter("@BYZD18", SqlDbType.VarChar,50),
                    new SqlParameter("@CardNO", SqlDbType.VarChar,50),
                    new SqlParameter("@CompanyPhone", SqlDbType.VarChar,50),
                    new SqlParameter("@CertiFicateType", SqlDbType.Int),
                    new SqlParameter("@AddressType", SqlDbType.Int),
                    new SqlParameter("@PhoneMessage", SqlDbType.Int),
                    new SqlParameter("@VC_STOP", SqlDbType.Int),
                    new SqlParameter("@CREDIT_FS", SqlDbType.Decimal)



            };
        parameters[0].Value = model.VC_CODE;
        parameters[1].Value = model.VC_LASTLOGINTIME;

        if (model.VC_PWDQUESTION != null)
            parameters[2].Value = model.VC_PWDQUESTION;
        else
            parameters[2].Value = DBNull.Value;


        if (model.VC_PWDANSWER != null)
            parameters[3].Value = model.VC_PWDANSWER;
        else
            parameters[3].Value = DBNull.Value;


        if (model.VC_PROVINCE != null)
            parameters[4].Value = model.VC_PROVINCE;
        else
            parameters[4].Value = DBNull.Value;


        if (model.VC_CITY != null)
            parameters[5].Value = model.VC_CITY;
        else
            parameters[5].Value = DBNull.Value;


        if (model.VC_AREA != null)
            parameters[6].Value = model.VC_AREA;
        else
            parameters[6].Value = DBNull.Value;


        if (model.VC_ADDRESS != null)
            parameters[7].Value = model.VC_ADDRESS;
        else
            parameters[7].Value = DBNull.Value;

        parameters[8].Value = model.BYZD1;
        parameters[9].Value = model.BYZD2;
        parameters[10].Value = model.BYZD3;
        parameters[11].Value = model.BYZD4;
        parameters[12].Value = model.BYZD5;
        parameters[13].Value = model.BYZD6;
        parameters[14].Value = model.BYZD7;
        parameters[15].Value = model.BYZD8;

        if (model.BYZD9 != null)
            parameters[16].Value = model.BYZD9;
        else
            parameters[16].Value = DBNull.Value;


        if (model.BYZD10 != null)
            parameters[17].Value = model.BYZD10;
        else
            parameters[17].Value = DBNull.Value;


        if (model.BYZD11 != null)
            parameters[18].Value = model.BYZD11;
        else
            parameters[18].Value = DBNull.Value;


        if (model.BYZD12 != null)
            parameters[19].Value = model.BYZD12;
        else
            parameters[19].Value = DBNull.Value;

           parameters[20].Value = model.BYZD13;

        if (model.BYZD14 != DateTime.MinValue)
            parameters[21].Value = model.BYZD14;
        else
            parameters[21].Value = DBNull.Value;


        if (model.CustomerLevel != null)
            parameters[22].Value = model.CustomerLevel;
        else
            parameters[22].Value = DBNull.Value;


        if (model.CreatDateTime != DateTime.MinValue)
            parameters[23].Value = model.CreatDateTime;
        else
            parameters[23].Value = DBNull.Value;

        if (model.VC_Note != null)
            parameters[24].Value = model.VC_Note;
        else
            parameters[24].Value = DBNull.Value;

        //
        if (model.VC_Note != null)
            parameters[25].Value = model.BYZD15;
        else
            parameters[25].Value = DBNull.Value;
        if (model.VC_Note != null)
            parameters[26].Value = model.BYZD16;
        else
            parameters[26].Value = DBNull.Value;
        if (model.VC_Note != null)
            parameters[27].Value = model.BYZD17;
        else
            parameters[27].Value = DBNull.Value;
        if (model.VC_Note != null)
            parameters[28].Value = model.BYZD18;
        else
            parameters[28].Value = DBNull.Value;
        if (model.VC_Note != null)
            parameters[29].Value = model.CardNO;
        else
            parameters[29].Value = DBNull.Value;
        if (model.VC_Note != null)
            parameters[30].Value = model.CompanyPhone;
        else
            parameters[30].Value = DBNull.Value;

        parameters[31].Value = model.CertiFicateType;
        parameters[32].Value = model.AddressType;
        parameters[33].Value = model.PhoneMessage;
        parameters[34].Value = model.VC_STOP;
        parameters[35].Value = model.CREDIT_FS;



        SqlHelper.RunProcedure(WEBConnectionString, "sp_V_CUSTOMER_AUTO_Update", parameters, out rowsAffected);
        string result = "";
        if (rowsAffected > 0)
        {
            result = "1";
        }
        else
        {
            result = "0";
        }
        return result;
    }
    #endregion

    #endregion

    #region 更新密码
    public static string UpPwd(string UserCode,string NewPwd)
    {
        return Up_ERP(UserCode, NewPwd) + Up_Web(UserCode, NewPwd);
    }
    private static string Up_ERP(string UserCode,string NewPwd)
    {
        int rowsAffected;
        string sql = "update V_Customer set BYZD7='" + NewPwd + "' where DM='" + UserCode + "'";
        rowsAffected = SqlHelper.ExecuteSql(ERPConnectionString, sql);
        if (rowsAffected > 0)
        {
            return "1";
        }
        else
        {
            return "0";
        }
    }
    private static string Up_Web(string UserCode, string NewPwd)
    {
        int rowsAffected;
        string sql = "update V_Customer set BYZD7='" + NewPwd + "' where DM='" + UserCode + "'";
        rowsAffected = SqlHelper.ExecuteSql(WEBConnectionString, sql);
        if (rowsAffected > 0)
        {
            return "1";
        }
        else
        {
            return "0";
        }
    }
    #endregion

    #region 更新结余款和可用发货金额
    public static bool UpdateBalanceAndUsableDeliverAmount(string[] strArrayToUpdateBStyle, string[] strToUpdateERP)
    {
        bool updateBStyleResult = UpdateBStyleDB(strArrayToUpdateBStyle);
        bool updateErpResult = UpdateErpDB(strToUpdateERP);
        if (updateBStyleResult == true && updateErpResult == true)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    public static bool UpdateBStyleDB(string[] strArrayToUpdateBStyle)
    {
        return SqlHelper.ExecuteNonQuery(WEBConnectionString, strArrayToUpdateBStyle);
    }

    public static bool UpdateErpDB(string[] strToUpdateERP)
    {
        return SqlHelper.ExecuteNonQuery(ERPConnectionString, strToUpdateERP);
    }

    #endregion

    #region 日结
    public static bool Write1(DataTable dt)
    {
        if (dt.Rows.Count > 0)
        {
            int count = dt.Rows.Count;
            string str = "";
            try
            {
                for (int j = 0; j < count; j++)
                {
                    str = " insert into BStyle_lsrj (LX, RQ, SDDM, SPDM, GG1DM, GG2DM, VIPDM, SL, ZK, DJ, CKJ, JE, BZJE, BZ, BYZD3,OrderID,KWDM) values "
                           + "(" + dt.Rows[j]["LX"].ToString() + ",'" + dt.Rows[j]["RQ"].ToString() + "','" + dt.Rows[j]["SDDM"].ToString() + "','" + dt.Rows[j]["SPDM"].ToString()
                            + "','" + dt.Rows[j]["GG1DM"].ToString() + "','" + dt.Rows[j]["GG2DM"].ToString() + "','" + dt.Rows[j]["VIPDM"].ToString() + "',"
                            + dt.Rows[j]["SL"].ToString() + "," + dt.Rows[j]["ZK"].ToString() + "," + dt.Rows[j]["DJ"].ToString() + "," + dt.Rows[j]["CKJ"].ToString() + ","
                            + dt.Rows[j]["JE"].ToString() + "," + dt.Rows[j]["BZJE"].ToString() + ",'" + dt.Rows[j]["BZ"].ToString() + "','" + dt.Rows[j]["BYZD3"].ToString() + "','" + dt.Rows[j]["OrderID"].ToString() + "','" + dt.Rows[j]["KWDM"].ToString() + "')";

                    SqlHelper.ExecuteSql(ERPConnectionString,str);
                }

                return true;
            }
            catch
            {
                return false;
            }

        }
        else
        {
            return false;
        }

    }
    public static bool Write2()
    {
        string ProcName = "";
        if (ConfigurationManager.AppSettings["DX"].ToString() == "0")
        {
            ProcName = "p_BStyle_lsrj ";
        }
        else
        {
            ProcName = "p_BStyle_lsrjDX ";
        }
        //sqlstr = "exec ss";
        try
        {
            DataSet ds = new DataSet();
            ds = SqlOld.ExecuteDataset(ERPConnectionString, CommandType.StoredProcedure, ProcName);
            int c = ds.Tables.Count;
            string res = ds.Tables[c - 1].Rows[0][0].ToString();
            if (res == "0")
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        catch
        {
            return false;
        }

    }
    public static bool Write3(DataTable dt)
    {
        string date = dt.Rows[0]["RQ"].ToString();
        string sqlstr = "";
        sqlstr = " declare @w int ;exec OC_Order_SaleState  '" + date + "',@w output;select @w ";
        //sqlstr = "exec  bb";
        try
        {
            if ((int)SqlOld.ExecuteScalar(WEBConnectionString, CommandType.Text, sqlstr) > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        catch
        {
            return false;
        }

    }
    #endregion

    #region 插入积分分成信息
    /// <summary>
    /// 插入积分分成信息
    /// </summary>
    /// <param name="l">Sql语句</param>
    /// <returns></returns>
    public static bool InsertIntergal(List<string> l)
    {
        bool result=true;
        foreach (string sql in l)
        {
            int x=SqlHelper.ExecuteSql(WEBConnectionString, sql);
            if (x < 1)
            {
                result = false;
            }
        }
        return result;
    }
    #endregion

    #region 更新会员积分
    public static void UpdateCustomerDqjf(string orderid)
    {
        string where = "IntergalName ='" + orderid + "'";
        List<Model.IntegralWasteBook> lists= new BLL.IntegralWasteBook().GetList(null, where, null);
        foreach(Model.IntegralWasteBook aa in lists)
        {
            string sql = "UPDATE [V_VIPSET]  SET [DQJF]=[DQJF]+" + aa.IntergalRange + "  WHERE GKDM='" + aa.IntergalExtendAttribute1 + "'";
            SqlHelper.ExecuteSql(ERPConnectionString, sql);
            SqlHelper.ExecuteSql(WEBConnectionString, sql);
        }
    }
    /// <summary>
    /// 更新会员积分
    /// </summary>
    /// <param name="l"></param>
    public static void UpdateCustomerDqjf(List<string> l)
    {
        foreach (string sql in l)
        {
           SqlHelper.ExecuteSql(WEBConnectionString, sql);
           SqlHelper.ExecuteSql(ERPConnectionString, sql);
        }
    }
    #endregion

    #region 更新订单积分分成状态
    public static void UpdateDDJFDivide(string orderid)
    {
        string sql = "UPDATE [ORDERDOC] SET ORDER_SendJF=1,ORDER_JFSendTime='"+DateTime.Now.ToString()+"' WHERE [ORDER_ID]='" + orderid + "'";
        SqlHelper.ExecuteSql(WEBConnectionString, sql);
    }
    #endregion
}
